package com.java.comprehensive.other.sort;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * 此题还可以引申至配对字符符匹配问题，如单引号，双引号匹配问题。
 *
 */

public class ExpStackMatching {

    public boolean matching(String expression) {
        if (expression == null || expression == "") {
            System.out.println("输入表达式为空或没有输入表达式");
        }

        Stack<Character> stack = new Stack<Character>();

        for (int index = 0; index < expression.length(); index++) {
            switch (expression.charAt(index)) {
                case '(':
                    stack.push(expression.charAt(index));
                    break;
                case '{':
                    stack.push(expression.charAt(index));
                    break;
                case '[':
                    stack.push(expression.charAt(index));
                    break;
                case ')':
                    if (!stack.empty() && stack.peek() == '(') {
                        stack.pop();
                    }
                    break;

                case '}':
                    if (!stack.empty() && stack.peek() == '{') {
                        stack.pop();
                    }
                case ']':
                    if (!stack.empty() && stack.peek() == '[') {
                        stack.pop();
                    }

            }
        }

        if (stack.empty())
            return true;
        return false;
    }

    public boolean matching_1(String expression) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < expression.length(); i++) {
            switch (expression.charAt(i)) {
                case '(':
                    stack.push(expression.charAt(i));
                    break;
                case '[':
                    stack.push(expression.charAt(i));
                    break;
                case '{':
                    stack.push(expression.charAt(i));
                    break;
                case ')':
                    if (!stack.isEmpty() && stack.peek().equals('(')) {
                        stack.pop();
                        break;
                    }
                case ']':
                    if (!stack.isEmpty() && stack.peek().equals('[')) {
                        stack.pop();
                        break;
                    }
                case '}':
                    if (!stack.isEmpty() && stack.peek().equals('{')) {
                        stack.pop();
                        break;
                    }
                default:
                    break;
            }

        }
        return stack.size() == 0;
    }
    //咖啡宝贝改良版
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        Map<Character, Character> map = new HashMap<>();
        map.put('{', '}');
        map.put('(', ')');
        map.put('[', ']');

        char[] chars = s.toCharArray();

        for (char aChar : chars) {

            if (stack.isEmpty()) {
                stack.add(aChar);
            } else {
                if (map.get(stack.peek()) != null && map.get(stack.peek()).equals(aChar)) {
                    stack.pop();
                } else {
                    stack.add(aChar);
                }
            }

        }

        return stack.isEmpty();
    }

    public static void main(String[] args) {

        String expression = "{[((1+3)+2+4)+9*7]}";
        String ex = "({}[[{]]})";

        ExpStackMatching oj = new ExpStackMatching();

        boolean flag = oj.matching_1(expression);

        if (flag) {
            System.out.println("匹配成功！");
        } else {
            System.out.println(" 匹配失败 ");
        }
    }

}